<?php

namespace app\api\controller;

use app\common\controller\Api;
use app\index\library\wechat\MiniApp;
use fast\Http;
use think\Cache;
use think\Hook;

class Adtf extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    public function index()
    {
        $sh = false; // 审核
        $path = input('path','pages/user/order/order'); // 小程序路径
        $order_no = input('order_no'); // 商品id
//        $appid = input('appid','wx6156277329fd0898');// 顶好鲜
        $appid = input('appid','wxc246efab1cd70443'); // 优优选购呀

        $query = [];
        if ($order_no){
            $query['order_no'] = $order_no;
        }
//        else{
//            $this->error('订单不存在');
//        }
        if (!$appid){
            $result = $this->getDomain($path,$query);

            if (!isset($result['url_link'])){
                $key = 'wanlshop_acctocken';
                Cache::store('redis')->rm($key);
                $result = $this->getDomain($path,$query);
            }
        }else{
            $result = $this->getOtherDomain($path,$query,$appid);

            if (!isset($result['url_link'])){
                $key = 'acctocken'.$appid;
                Cache::store('redis')->rm($key);
                $result = $this->getOtherDomain($path,$query,$appid);
            }
        }

        // $result['url_link'];
        $this->success('',['url'=>$result['url_link']??'','sh'=>$sh]);
    }

    // 获取小程序跳转域名
    public function getDomain($path,$query = [])
    {
        $config = get_addon_config('wanlshop');
        $appid = $config['mp_weixin']['appid'];
        $secret = $config['mp_weixin']['appsecret'];

        $token = MiniApp::getAccessToken($appid,$secret);
        $query = http_build_query($query);

        $data = [
            "path" => $path,
            "query" => $query,
            "expire_type" => 1,
            "expire_interval" => 1,
        ];

        $options = [
            CURLOPT_HTTPHEADER => array('Content-type: application/json')
        ];

        $result = Http::post("https://api.weixin.qq.com/wxa/generate_urllink?access_token=$token",json_encode($data),$options);

        return json_decode($result,true);

    }

    public function getOtherDomain($path,$query = [],$q_appid)
    {
        $config = get_addon_config('wanlshop');
        $appid = $config['mp_weixin']['appid'];
        $secret = $config['mp_weixin']['appsecret'];

        if ($q_appid && $q_appid != $appid){
            $AppData = MiniApp::getAppInfo($q_appid);
            if ($AppData){
                $appid = $AppData['appid'];
                $secret = $AppData['secret_key'];
            }
        }


        $token = MiniApp::getOtherAccessToken($appid,$secret,'acctocken'.$appid);
        $query = http_build_query($query);

        $data = [
            "path" => $path,
            "query" => $query,
            "expire_type" => 1,
            "expire_interval" => 1,
        ];

        $options = [
            CURLOPT_HTTPHEADER => array('Content-type: application/json')
        ];

        $result = Http::post("https://api.weixin.qq.com/wxa/generate_urllink?access_token=$token",json_encode($data),$options);

        return json_decode($result,true);

    }

}